from random import *
from visual import *
import coords
import SteeringBehaviours

class Ship:
    
    def __init__(self, position, map, name="A SHiP", aRadius=2, pRadius=1, mass=1, velocity=0, heading=[0,0], MAX_speed = 1, MAX_force= 1, MAX_turnRate=1):
        self.activeRadius = aRadius
        self.passiveRadius = pRadius
        self.position = position
        self.myGridCoords = [0,0]
        self.velocity = velocity
        self.mass = mass
        self.heading = heading
        self.MAX_speed = MAX_speed
        self.MAX_force = MAX_force
        self.MAX_turnRate = MAX_turnRate
        self.Steering = SteeringBehaviours.SteeringBehaviours(self)
        self.State = ""
        self.myList = [5,4]
        self.map = map
        self.lastCellCoords = [-1,-1]
        self.path = None
        self.pathCount = 0
        
    def update(self):
        
        targetPos = self.GetDesiredLocation()
        
        self.steeringForce = self.Steering.Calculate(targetPos)
            
        self.acceleration = self.steeringForce / self.mass
        
        self.accelerationGrav += self.acceleration
    
        self.velocity += self.accelerationGrav * time
            
        self.velocity = self.SteeringInterface.Truncate(self, self.velocity)
        
        self.position += self.velocity * time
        
        self.updateGridCoords()
            
        if mag(self.velocity)**2 > 0.0001:
            self.heading = norm(self.velocity)  
            self.heading.y = 0

    def getGridCoords(self):
        return self.myGridCoords
    
    def updateGridCoords(self):
          #Get grid location from screen coords
          #position = ((self.position[0] + 7)-51) , ((self.position[1] + 7)-51)
          #if position[0] > 0:
          #    self.myGridCoords[0] = position[0]//16
          #if position[1] > 0:
          #    self.myGridCoords[1] = position[1]//16
          #print str(self.myGridCoords[0])+","+str(self.myGridCoords[1])
          
          if type(self.position) == type(self.myList):
              self.myGridCoords = coords.screenToGridList(self, self.position)
          else:
              self.myGridCoords = coords.screenToGrid(self, self.position)
          return self.myGridCoords
        
    def GetDesiredLocation(self):
        pass
        
    def Render(self):
        pass

    def getActiveRadius(self):
        return self.activeRadius
    
    def getPassiveRadius(self):
        return self.passiveRadius
    
    def getPosition(self):
        return self.position
    
    def getVelocity(self):
        return self.velocity
    
    def getMass(self):
        return self.mass
    
    def getHeading(self):
        return self.velocity
    
    def getMaxSpeed(self):
        return self.MAX_speed
    
    def getMaxForce(self):
        return self.MAX_force
    
    def getMaxTurnRate(self):
        return self.MAX_turnRate
    
    def setTarget(self, targetPos):
        self.desiredLocation = targetPos